package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.FoodBase;
import com.itheima.service.KnowledgeService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/uploadknowledge")
public class KnowledgeController {

    @Reference
    private KnowledgeService knowledgeService;



    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            /*此处已测试文件上传至controller层成功*/
            /*POIUtils.readExcel(excelFile)把每行中的值作为一个数组，所有行作为一个集合返回*/
            List<String[]> strings = POIUtils.readExcel(excelFile);/*读取了上传的文件*/
            /*将文件中的没一行封装为FoodBase对象*/
            List<FoodBase> data = new ArrayList<>();
            for (String[] string : strings) {
                FoodBase foodBase=new FoodBase();
                foodBase.setId(string[0]);
                foodBase.setFoodParentId(string[1]);
                foodBase.setFoodName(string[2]);
                foodBase.setFoodtype(string[3]);
                foodBase.setFoodTip(string[4]);
                foodBase.setFoodEated(string[5]);
                foodBase.setFoodWater(string[6]);
                foodBase.setFoodKcal(string[7]);
                foodBase.setFoodKJ(string[8]);
                foodBase.setFoodProtein(string[9]);
                foodBase.setFoodFat(string[10]);
                foodBase.setFoodCho(string[11]);
                foodBase.setFoodWeight(string[12]);
                foodBase.setGxy(string[13]);
                foodBase.setTnb(string[14]);
                foodBase.setGxb(string[15]);
                foodBase.setNcz(string[16]);
                foodBase.setXnxgjb(string[17]);
                foodBase.setXzyc(string[18]);
                foodBase.setFp(string[19]);
                foodBase.setTf(string[20]);
                foodBase.setTnbsb(string[21]);
                foodBase.setFoodUnit(string[22]);
                foodBase.setUnitBulk(string[23]);
                foodBase.setRegionID(string[24]);
                foodBase.setGdjb(string[25]);
                data.add(foodBase);
            }


            knowledgeService.add(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = knowledgeService.pageQuery(queryPageBean);
        return pageResult;
    }

}
